package com.hurl.waguji.biz.download;

import java.util.Date;
import java.util.List;

import org.testng.log4testng.Logger;

import com.hurl.library.spider.UrlPattern;
import com.hurl.library.util.StringUtils;
import com.hurl.waguji.base.Formatter;
import com.hurl.waguji.base.dataprocess.ProcessParams;
import com.hurl.waguji.persistence.DmGpBO;
import com.hurl.waguji.persistence.JcsjGsggThsBO;
import com.hurl.waguji.persistence.JcsjGsggThsBOId;

class ProfileGsggThsProcessor extends BaseDownloadForUpdateProcessor{

	public ProfileGsggThsProcessor(ProcessParams params,
			DownloadIdentifier identifier) {
		super(params, identifier);
	}
	private static final Logger log = Logger.getLogger(ProfileGsggThsProcessor.class);
	
	@Override
	public void convertHTML2BO(String url, String html, BOSaver collector) {
		log.debug(html);
		
		if(StringUtils.isBlank(html)){
			return;
		}

		String div = StringUtils.substringBetween(html, "id=\"glcjj\">管理层简介</div>", "value=\"高管介绍\" id=\"tagName\"");
		if(StringUtils.isBlank(div)){
			return;
		}

		JcsjGsggThsBO bo = null;
		String xm = null;
		String xb = null;
		String xl = null;
		Integer nl = null;
		String zw = null;
		String rq = null;
		Date rqqD = null;
		Date rqzD = null;
		String jj = null;
		List<String> tablelist = StringUtils.substringsBetween(div, "class=\"ggjs_frame_1\">", "</table><table");
		if(tablelist!=null && !tablelist.isEmpty()){
			for(String table : tablelist){
				/*解析HTML*/
				xm = Formatter.toString(StringUtils.substringBetween(table, "class=\"ggjs_frame_2\" colspan=\"3\">", "</td>"));
				xb = Formatter.toString(StringUtils.substringBetween(table, "性别: <span>", "</span>"));
				xl = Formatter.toString(StringUtils.substringBetween(table, "学历: <span>", "</span>"));
				xl = StringUtils.isBlank(xl)?"":xl;
				nl = Formatter.toInteger((StringUtils.substringBetween(table, "年龄: <span>", "</span>")));
				zw = Formatter.toString(StringUtils.substringBetween(table, "职务: <span>", "</span>"));
				rq = StringUtils.substringBetween(table, "本届任期: <span>", "</span>");
				rqqD = null;
				rqzD = null;
				if(StringUtils.isNotBlank(rq)){
					String[] rqarr = rq.split("至");
					if(rqarr.length>0){
						rqqD = Formatter.toDate(rqarr[0]);
					}
					if(rqarr.length>1){
						rqzD = Formatter.toDate(rqarr[1]);
					}
				}
				jj = Formatter.toString(StringUtils.substringBetween(table, "class=\"ggjs_frame_4\"><span>", "</span>"));
				
				bo = new JcsjGsggThsBO();
				bo.setId(new JcsjGsggThsBOId(context.getEnvAsString("gpdm"),xm,nl,xb,xl));
				bo.setRqq(rqqD);
				bo.setRqz(rqzD);
				bo.setZw(zw);
				bo.setJj(jj);
				bo.setLastupdate(currentTime);
				
				collector.addBO(bo);
			}
		}
	
	}
	@Override
	public UrlPattern getUrlPattern() {
		return new StockIteratorUrlPattern(context) {
			
			@Override
			public String bo2url(DmGpBO bo) {
				return "http://basic.10jqka.com.cn/"+bo.getGpDmJ()+"/ggjs.html";
			}
		};
	}

}
